package com.example.m2m.servlet;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.example.m2m.pojo.User;
import com.example.m2m.service.UserRoleService;
import com.example.m2m.service.impl.UserRoleServiceImpl;
import com.example.m2m.util.ThUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.web.IWebExchange;
import org.thymeleaf.web.servlet.JakartaServletWebApplication;

@WebServlet(urlPatterns = {"/list","/toadd","/save","/del","/find","/findUser"})
public class HelloServlet extends HttpServlet {

    private static  final UserRoleService SERVICE = new UserRoleServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        System.out.println(servletPath);
        IWebExchange webExchange = JakartaServletWebApplication.buildApplication(getServletContext()).buildExchange(req, resp);
        WebContext webContext = new WebContext(webExchange);
        Context context = new Context();
        PrintWriter out = resp.getWriter();
//创建一个日期转换器
        DateConverter dateConverter = new DateConverter();
//设置格式
        dateConverter.setPatterns(new String[]{"yyyy-MM-dd"});
//注册格式
        ConvertUtils.register(dateConverter, Date.class);
        switch (servletPath){
            case "/find"->{
                String id = req.getParameter("id");
//                webContext.setVariable("roles",SERVICE.roles());
//                webContext.setVariable("user",SERVICE.findUser(id));
                context.setVariable("id",id);
                ThUtils.print("/view/update.html",context,resp);
            }
            case "/findUser"->{
                String id = req.getParameter("id");
                Map map = new HashMap();
                map.put("roles",SERVICE.roles());
                map.put("user",SERVICE.findUser(id));
                out.print(JSON.toJSONString(map));
                out.close();
            }

            case "/del" ->{
                String ids = req.getParameter("ids");
                out.print(JSON.toJSONString(SERVICE.delete(ids)));
                out.close();
            }
            case "/toadd"->{

                webContext.setVariable("roles",SERVICE.roles());
                ThUtils.print("/view/add.html",webContext,resp);
            }
             case "/list"->{
                 String offset = req.getParameter("offset");
                 webContext.setVariable("pager",SERVICE.users(offset));
                 ThUtils.print("/view/list.html",webContext,resp);
             }
             case "/save" ->{
                 String[] ids = req.getParameterValues("ids");
                 Map<String, String[]> map = req.getParameterMap();
                 User user= new User();
                 try {
                     BeanUtils.populate(user,map);
                     System.out.println(user);
                     out.print(JSON.toJSONString(SERVICE.saveUser(user,ids)));
                     out.close();
                 } catch (IllegalAccessException e) {
                     throw new RuntimeException(e);
                 } catch (InvocationTargetException e) {
                     throw new RuntimeException(e);
                 }

             }
        }


    }
}
